package com.classic.okhttp.services;

import com.classic.okhttp.Cons;
import android.app.Activity;
import com.classic.okhttp.base.OkHttpUtils;
import com.classic.okhttp.zmoumall.utils.MD5;
import com.classic.okhttp.zmoumall.http.ACache;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.classic.okhttp.zmoumall.http.ObjectCallback;
import okhttp3.MediaType;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;

import com.classic.okhttp.RequestBeans.HVAddVenueOrderListRequestBean;
import com.classic.okhttp.RequestBeans.HVAddProductOrderListRequestBean;
import com.classic.okhttp.beans.HVAddOrderBean;
import com.classic.okhttp.beans.HVMyOrderInfoBean;
import com.classic.okhttp.beans.HVOrderCardBean;


import com.classic.okhttp.enums.Enum;

public class IOrderService
{

	private static String _getFullURL(String method)
	{
		return new StringBuffer( Cons.GATEWAY_URL ).append("order/").append(method).toString();
	}

	//生成订单信息
	public static void addOrder(Activity activity, Enum.EnumCollect orderType, HVAddVenueOrderListRequestBean venue, HVAddProductOrderListRequestBean product, boolean isCache, ObjectCallback<String> callback)
	{
		callback.setCallBackBean(new TypeToken<String>(){}.getType());
		callback.setContext(activity);

		Map<String, Object> params = new HashMap<>();
		params.put("orderType", orderType.value);
		params.put("venue", venue);
		params.put("product", product);

		Gson gson=new Gson();
		String json=gson.toJson(params);

		String url = _getFullURL("addOrder");

		if(isCache) {//添加缓存
			String key = MD5.MD5Encode(new StringBuffer(url).append(orderType).append(venue).append(product).toString());
			ACache aCache = ACache.get(activity);
			callback.setACache(aCache, key, isCache);
		}

		OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json;charset=utf-8"))
               	.url(url)
                .content(json)
                .addHeader("Token",Cons.mToken)
                .addHeader("DeviceID",Cons.mDeviceId)
				.addHeader("DeviceType",Cons.mDeviceType)
				.addHeader("Version",Cons.mVersion)
				.addHeader("VersionCode",Cons.mVersionCode)
				.addHeader("Data",Cons.getData(json))
				.addHeader("AppType",Cons.getAppType())
                .tag(activity)
                .build()
                .execute(callback);
	}

	//下订单
	public static void addOrderV2(Activity activity, Enum.EnumCollect orderType, HVAddVenueOrderListRequestBean venue, HVAddProductOrderListRequestBean product, boolean isCache, ObjectCallback<HVAddOrderBean> callback)
	{
		callback.setCallBackBean(new TypeToken<HVAddOrderBean>(){}.getType());
		callback.setContext(activity);

		Map<String, Object> params = new HashMap<>();
		params.put("orderType", orderType.value);
		params.put("venue", venue);
		params.put("product", product);

		Gson gson=new Gson();
		String json=gson.toJson(params);

		String url = _getFullURL("addOrderV2");

		if(isCache) {//添加缓存
			String key = MD5.MD5Encode(new StringBuffer(url).append(orderType).append(venue).append(product).toString());
			ACache aCache = ACache.get(activity);
			callback.setACache(aCache, key, isCache);
		}

		OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json;charset=utf-8"))
               	.url(url)
                .content(json)
                .addHeader("Token",Cons.mToken)
                .addHeader("DeviceID",Cons.mDeviceId)
				.addHeader("DeviceType",Cons.mDeviceType)
				.addHeader("Version",Cons.mVersion)
				.addHeader("VersionCode",Cons.mVersionCode)
				.addHeader("Data",Cons.getData(json))
				.addHeader("AppType",Cons.getAppType())
                .tag(activity)
                .build()
                .execute(callback);
	}

	//获取订单详情
	public static void getOrderInfo(Activity activity, Enum.EnumCollect orderType, String orderId, boolean isCache, ObjectCallback<HVMyOrderInfoBean> callback)
	{
		callback.setCallBackBean(new TypeToken<HVMyOrderInfoBean>(){}.getType());
		callback.setContext(activity);

		Map<String, Object> params = new HashMap<>();
		params.put("orderType", orderType.value);
		params.put("orderId", orderId);

		Gson gson=new Gson();
		String json=gson.toJson(params);

		String url = _getFullURL("getOrderInfo");

		if(isCache) {//添加缓存
			String key = MD5.MD5Encode(new StringBuffer(url).append(orderType).append(orderId).toString());
			ACache aCache = ACache.get(activity);
			callback.setACache(aCache, key, isCache);
		}

		OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json;charset=utf-8"))
               	.url(url)
                .content(json)
                .addHeader("Token",Cons.mToken)
                .addHeader("DeviceID",Cons.mDeviceId)
				.addHeader("DeviceType",Cons.mDeviceType)
				.addHeader("Version",Cons.mVersion)
				.addHeader("VersionCode",Cons.mVersionCode)
				.addHeader("Data",Cons.getData(json))
				.addHeader("AppType",Cons.getAppType())
                .tag(activity)
                .build()
                .execute(callback);
	}

	//取消订单
	public static void cancelOrder(Activity activity, Enum.EnumCollect orderType, String orderId, boolean isCache, ObjectCallback<Boolean> callback)
	{
		callback.setCallBackBean(new TypeToken<Boolean>(){}.getType());
		callback.setContext(activity);

		Map<String, Object> params = new HashMap<>();
		params.put("orderType", orderType.value);
		params.put("orderId", orderId);

		Gson gson=new Gson();
		String json=gson.toJson(params);

		String url = _getFullURL("cancelOrder");

		if(isCache) {//添加缓存
			String key = MD5.MD5Encode(new StringBuffer(url).append(orderType).append(orderId).toString());
			ACache aCache = ACache.get(activity);
			callback.setACache(aCache, key, isCache);
		}

		OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json;charset=utf-8"))
               	.url(url)
                .content(json)
                .addHeader("Token",Cons.mToken)
                .addHeader("DeviceID",Cons.mDeviceId)
				.addHeader("DeviceType",Cons.mDeviceType)
				.addHeader("Version",Cons.mVersion)
				.addHeader("VersionCode",Cons.mVersionCode)
				.addHeader("Data",Cons.getData(json))
				.addHeader("AppType",Cons.getAppType())
                .tag(activity)
                .build()
                .execute(callback);
	}

	//更新订单优惠券信息
	public static void updateOrderCouponInfo(Activity activity, String orderId, Integer couponUserId, String couponId, Double money, boolean isCache, ObjectCallback<HVOrderCardBean> callback)
	{
		callback.setCallBackBean(new TypeToken<HVOrderCardBean>(){}.getType());
		callback.setContext(activity);

		Map<String, Object> params = new HashMap<>();
		params.put("orderId", orderId);
		params.put("couponUserId", couponUserId);
		params.put("couponId", couponId);
		params.put("money", money);

		Gson gson=new Gson();
		String json=gson.toJson(params);

		String url = _getFullURL("updateOrderCouponInfo");

		if(isCache) {//添加缓存
			String key = MD5.MD5Encode(new StringBuffer(url).append(orderId).append(couponUserId).append(couponId).append(money).toString());
			ACache aCache = ACache.get(activity);
			callback.setACache(aCache, key, isCache);
		}

		OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json;charset=utf-8"))
               	.url(url)
                .content(json)
                .addHeader("Token",Cons.mToken)
                .addHeader("DeviceID",Cons.mDeviceId)
				.addHeader("DeviceType",Cons.mDeviceType)
				.addHeader("Version",Cons.mVersion)
				.addHeader("VersionCode",Cons.mVersionCode)
				.addHeader("Data",Cons.getData(json))
				.addHeader("AppType",Cons.getAppType())
                .tag(activity)
                .build()
                .execute(callback);
	}
}
